package com.ruoyi.web.controller.Region;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.exception.user.CaptchaExpireException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.OlivePlantCate;
import com.ruoyi.system.service.IOlivePlantCateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 存放种植区的种类Controller
 *
 * @author ruoyi
 * @date 2024-10-30
 */
@RestController
@RequestMapping("/region/cate")
public class OlivePlantCateController extends BaseController {
    @Autowired
    private IOlivePlantCateService olivePlantCateService;

    /**
     * 查询存放种植区的种类列表
     */
    @PreAuthorize("@ss.hasPermi('system:cate:list')")
    @GetMapping("/list")
    public TableDataInfo list(OlivePlantCate olivePlantCate) {
        startPage();
        List<OlivePlantCate> list = olivePlantCateService.selectOlivePlantCateList(olivePlantCate);
        return getDataTable(list);
    }

    /**
     * 导出存放种植区的种类列表
     */
    @PreAuthorize("@ss.hasPermi('system:cate:export')")
    @Log(title = "存放种植区的种类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OlivePlantCate olivePlantCate) {
        List<OlivePlantCate> list = olivePlantCateService.selectOlivePlantCateList(olivePlantCate);
        ExcelUtil<OlivePlantCate> util = new ExcelUtil<OlivePlantCate>(OlivePlantCate.class);
        util.exportExcel(response, list, "存放种植区的种类数据");
    }

    /**
     * 获取存放种植区的种类详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:cate:query')")
    @GetMapping(value = "/{cateId}")
    public AjaxResult getInfo(@PathVariable("cateId") Long cateId) {
        return success(olivePlantCateService.selectOlivePlantCateByCateId(cateId));
    }

    /**
     * 新增存放种植区的种类
     */
    @PreAuthorize("@ss.hasPermi('system:cate:add')")
    @Log(title = "存放种植区的种类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OlivePlantCate olivePlantCate) {
        if(!olivePlantCateService.checkOlivePlantNameUnique(olivePlantCate)){
            return error("新增类别"+olivePlantCate.getCateName()+"'失败，类别已存在'");
        }
        olivePlantCate.setUpdateBy(getUsername());
        return toAjax(olivePlantCateService.insertOlivePlantCate(olivePlantCate));

//            try {
//                int result = olivePlantCateService.insertOlivePlantCate(olivePlantCate);
//                return toAjax(result);
//            } catch (DuplicateKeyException e) {
//                return error("新增分类"+olivePlantCate.getCateName()+"'失败，分类编码已存在'");
//            } catch (Exception e) {
//                return AjaxResult.error("新增类别失败，系统出现错误");
//            }
    }

    /**
     * 修改存放种植区的种类
     */
    @PreAuthorize("@ss.hasPermi('system:cate:edit')")
    @Log(title = "存放种植区的种类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OlivePlantCate olivePlantCate) {
//        if(!olivePlantCateService.checkOlivePlantNameUnique(olivePlantCate)){
//            return error("新增类别"+olivePlantCate.getCateName()+"'失败，类别已存在'");
//        }
        olivePlantCate.setUpdateBy(getUsername());
        return toAjax(olivePlantCateService.updateOlivePlantCate(olivePlantCate));
    }

    /**
     * 删除存放种植区的种类
     */
    @PreAuthorize("@ss.hasPermi('system:cate:remove')")
    @Log(title = "存放种植区的种类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{cateIds}")
    public AjaxResult remove(@PathVariable Long[] cateIds) {
        return toAjax(olivePlantCateService.deleteOlivePlantCateByCateIds(cateIds));
    }
}
